package com.example.chat.service.impl;

import com.example.chat.model.AssetsRequestRecord;
import com.example.chat.model.Employee;
import com.example.chat.model.ro.AssetsRequestApprovalPassDistributeRO;
import com.example.chat.model.ro.AssetsRequestRecordRO;
import com.example.chat.repository.AssetsRequestRecordRepository;
import com.example.chat.repository.EmployeeRepository;
import com.example.chat.service.AssetsApprovalService;
import com.example.chat.service.AssetsService;
import com.example.chat.type.ApprovalStatus;
import com.example.chat.type.AssetsRequestApprovalPassType;
import com.example.chat.utils.ChatException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
 * @Description
 * @Date 2022/11/18 10:35
 * @Author pq
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AssetsApprovalServiceImpl implements AssetsApprovalService {

    @Resource
    private AssetsRequestRecordRepository assetsRequestRecordRepository;

    @Resource
    private EmployeeRepository employeeRepository;

    /**
     * @Author pq
     * @Description 固资申请通过-设备分配
     * @Date 11:23 2022/11/18
     * @Param [ro, currentEmpId]
     * @return void
     **/
    @Override
    public void passAssetsRequestByDistribute(AssetsRequestApprovalPassDistributeRO ro, String currentEmpId) {
        AssetsRequestRecord assetsRequestRecord=assetsRequestRecordRepository.findByIdAndDeletedIsFalse(ro.getAssetsRequestRecordId()).orElseThrow(()->new ChatException("当前固资申请单不存在"));
        Employee approvalEmp=employeeRepository.findByIdAndDeletedIsFalse(currentEmpId).orElseThrow(()->new ChatException("当前申请人不存在"));
        assetsRequestRecord.setApprovalStatus(ApprovalStatus.SUCCESS);
        assetsRequestRecord.setSubmitEmp(approvalEmp);
        assetsRequestRecord.setApprovalEmpId(approvalEmp.getId());
        assetsRequestRecord.setApprovalEmpName(approvalEmp.getName());
        assetsRequestRecord.setRejectReasonDescription(ro.getRejectReason());
        if( Objects.equals(ro.getAssetsRequestApprovalPassType(), AssetsRequestApprovalPassType.DISTRIBUTE_ASSETS)){
            assetsRequestRecord.setRequestAssetsId(ro.getAssetsId());
        }
        assetsRequestRecordRepository.save(assetsRequestRecord);
    }
}
